package data;

import entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 * @title: UserDao
 * @projectName BMS
 * @description: UserDao
 * @date 2024/12/20 23:45
 */
public class UserDao {
    /**
     * 根据用户名和密码获取用户信息
     * @param name
     * @param pass
     * @return
     */
    public static User getUser(String name, String pass) {
        User user = null;
        if(name != null && pass != null) {
            String sql = "select * from user where name = '" + name + "' and pass = '" + pass + "'";
            //执行查询语句返回结果集
            ResultSet rs = BaseDao.executeQuery(sql);
            try{
                if(rs.next()){
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setName(rs.getString("name"));
                    user.setPass(rs.getString("pass"));
                    user.setIs_admin(rs.getByte("is_admin"));
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return user;
    }
    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    public static User getUser(int id){
        User user = null;
        if(id > 0){
            String sql = "select * from user where id = '" + id + "'";
            ResultSet rs = BaseDao.executeQuery(sql);
            try{
                if(rs.next()){
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setName(rs.getString("name"));
                    user.setPass(rs.getString("pass"));
                    user.setIs_admin(rs.getByte("is_admin"));
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return user;
    }
    /**
     * 通过用户名 获取用户
     * @param name
     * @return
     */
    public static User getUser(String name) {
        User user = null;
        if(name != null && !name.equals("")){
            String sql = "select * from user where name = '" + name + "'";
            System.out.println("此时sql" + sql);
            ResultSet rs = BaseDao.executeQuery(sql);
            try{
                if(rs.next()){
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setName(rs.getString("name"));
                    user.setPass(rs.getString("pass"));
                    user.setIs_admin(rs.getByte("is_admin"));
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return user;
    }
    /**
     * 查询所有用户对象 并返回用户对象列表
     * @return
     */
    public static List<User> getUserList() {
        User user = null;
        List<User> list = new ArrayList<User>();
        String sql = "select * from user";
        ResultSet rs = BaseDao.executeQuery(sql);
        try{
            while(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setPass(rs.getString("pass"));
                user.setIs_admin(rs.getByte("is_admin"));
                list.add(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 添加新用户
     * @param user
     * @return
     */
    public static int insertUser(User user){
        if(user != null){
            int id = user.getId();
            String name = user.getName();
            String pass = user.getPass();
            byte is_admin = user.getIs_admin();
            String sql = "insert into user values("+id+","+name+","+pass+","+is_admin+")";
            return BaseDao.executeUpdate(sql);
        }else{
            return -1;
        }
    }
    /**
     * 修改指定用户信息
     * @param user
     * @return
     */
    public static int updateUser(User user){
        if(user != null){
            int id = user.getId();
            String name = user.getName();
            String pass = user.getPass();
            byte is_admin = user.getIs_admin();
            String sql = "update user set name="+name+",pass="+pass+",is_admin="+is_admin+" where id="+id;
            return BaseDao.executeUpdate(sql);
        }else{
            return -1;
        }
    }
    /**
     * 修改指定用户密码
     * @param name
     * @param pass
     * @return
     */
    public static int updatePass(String name, String pass){
        if(name != null && pass != null){
            String sql = "update user set pass=" + pass + "where name=" + name;
            return BaseDao.executeUpdate(sql);
        }else {
            return -1;
        }
    }
    /**
     * 删除指定id用户
     * @param id
     * @return
     */
    public static int deleteUser(int id){
        String sql = "delete from user where id=" + id;
        return BaseDao.executeUpdate(sql);
    }
    /**
     * 删除所有用户
     */
    public static void emptyUser(){
        String sql = "delete from user";
        BaseDao.executeUpdate(sql);
    }
}
